//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    internal class TableProperties : IOfficeXmlSerializable
    {
        internal const string xmlName = "tblPr";
        private bool bandCol;
        private bool bandRow;
        private EffectProperties effectProperties;
        private OfficeArtExtensionList extLst;
        private FillProperties fillProperties;
        private bool firstCol;
        private bool firstRow;
        private bool lastCol;
        private bool lastRow;
        private bool rtl;
        private TableStyle tableStyle;
        private Guid? tableStyleId;

        #region IOfficeXmlSerializable Members

        /// <summary>
        /// Generates an object from its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                rtl = c.GetOptionalBoolean("rtl", false);
                firstRow = c.GetOptionalBoolean("firstRow", false);
                firstCol = c.GetOptionalBoolean("firstCol", false);
                lastRow = c.GetOptionalBoolean("lastRow", false);
                lastCol = c.GetOptionalBoolean("lastCol", false);
                bandRow = c.GetOptionalBoolean("bandRow", false);
                bandCol = c.GetOptionalBoolean("bandCol", false);
                fillProperties = FillProperties.ReadFillProperties(c);
                effectProperties = EffectProperties.ReadEffectProperties(c);
                tableStyle = c.ReadOptionalElement<TableStyle>(TableStyle.xmlName);
                if
                    (tableStyle == null)
                {
                    if (c.ReaderLocalName == "tableStyleId")
                    {
                        tableStyleId = new Guid(c.ReadElementString("tableStyleId"));
                    }
                }
                extLst = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
            }
        }

        /// <summary>
        /// Convert an object into its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetOptionalBoolean("rtl", rtl, false);
                c.SetOptionalBoolean("firstRow", firstRow, false);
                c.SetOptionalBoolean("firstCol", firstCol, false);
                c.SetOptionalBoolean("lastRow", lastRow, false);
                c.SetOptionalBoolean("lastCol", lastCol, false);
                c.SetOptionalBoolean("bandRow", bandRow, false);
                c.SetOptionalBoolean("bandCol", bandCol, false);
                c.WriteOptionalElement(fillProperties);
                c.WriteOptionalElement(effectProperties);
                if (tableStyle != null)
                    c.WriteElement(tableStyle);
                else if (tableStyleId != null)
                {
                    c.WriteElementString("tableStyleId", "{"+tableStyleId.Value.ToString() + "}");
                }
                c.WriteOptionalElement(extLst);
            }
        }

        #endregion
    }
}